package analyze;

import java.util.ArrayList;

import logging.Logger;

import object.Connection;
import object.Node;

public class AnalyzeStatistics extends AnalyzeBase {
	
	@Override
	public ArrayList<AnalyzeInfo> Analyze(ArrayList<Node> nodes) {
		
		Logger.getInstance().log(Logger.TRACE, "Starting Network Statisitics"); 
		
		ArrayList<AnalyzeInfo> Messages = new ArrayList<AnalyzeInfo>();
		
		
		//ArrayList<AnalyzeInfo> connectStats = new ArrayList<AnalyzeInfo>();
		
		
		if(nodes.isEmpty())
		{
			Messages.add(new AnalyzeInfo(0,"Error: ", "No nodes to compare"));
			return Messages;
		}
		
		ArrayList<Node> globalVisited = new ArrayList<Node>();
		ArrayList<Connection> connections = new ArrayList<Connection>();
		ArrayList<Node> toVisit = new ArrayList<Node>();
		
	
		int singleCount = 0;
		int networkCount = 0;
		double maxlength = -1;
		double minlength = -1;
		double averagelength = 0;
		int nodeCount = 0;
		for( Node n : nodes )
		{
			if( globalVisited.contains(n) )
				continue;
			
			toVisit.add(n);
			int numFollowed = 0;
			int numToVisit = 1;
			networkCount++;
			
			while( numFollowed != numToVisit )
			{
				Node current = toVisit.get(numFollowed);
				nodeCount++;
				if( current.getConnections().isEmpty() )
				{
					singleCount++;
					networkCount--;					
				}
				else
				{
					for( Connection c : current.getConnections() )
					{
						if( connections.contains(c) )
							continue;
						else 
						{
							connections.add(c);
							
							if( toVisit.contains(c.getN1()) == false )
							{
								toVisit.add(c.getN1());
								numToVisit++;
							}
							else if ( toVisit.contains(c.getN2()) == false )
							{
								toVisit.add(c.getN2());
								numToVisit++;
							}
							
							double length = c.getLength();
							
							//We are adding each connection to be printed out. 
							//The first offset makes sure the the connections are printed at the end of the list
							Messages.add( new AnalyzeInfo(nodeCount+nodes.size()*5, " (" +c.getN1X()+"," + c.getN1Y()+") - (" +c.getN2X()+"," + c.getN2Y()+")", length));
							
							if( length > maxlength || maxlength == -1)
								maxlength = length;
							if( length < minlength || minlength == -1)
								minlength = length;
							averagelength = averagelength + length;
							
						}
					}
					
				}
				numFollowed++;
				
			}
			if( numFollowed!=1 )	//If a network was found, display the results
			{
				averagelength = averagelength / connections.size();
				Messages.add( new AnalyzeInfo(networkCount, "Network number", networkCount));
				Messages.add( new AnalyzeInfo(networkCount, "Number of Nodes", numFollowed));
				Messages.add( new AnalyzeInfo(networkCount, "Maximum length", maxlength ));
				Messages.add( new AnalyzeInfo(networkCount, "Minimum length", minlength));
				Messages.add( new AnalyzeInfo(networkCount, "Average Length", averagelength));
			}
						
			
			connections.clear();
			globalVisited.addAll(toVisit);
			toVisit.clear();
			maxlength = -1;
			minlength = -1;
			averagelength = 0;
			
		}
		Messages.add( new AnalyzeInfo(0, "Number of Networks", networkCount));
		Messages.add( new AnalyzeInfo(0, "Num Single Nodes", singleCount) );
		
		
		
		Logger.getInstance().log(Logger.TRACE, "Ending Network Statisitics");
		
		return Messages;
	}

}
